<?php
class Node_model_extends extends Model {
    private $table_node = 'node';
    private $table_mapping = 'n_mapping';
    
    private $table_node_format = array(
        'id'            => DB_DATA_INT,
        'allow_comment' => DB_DATA_INT,
        'is_public'     => DB_DATA_INT,
        'user_id'       => DB_DATA_INT,
        'rank'          => DB_DATA_INT,
        'changed'       => DB_DATA_INT,
        'created'       => DB_DATA_INT
    );
    
    /* --- public functions --- */
    public function edit($data) {
        $db_data = array();
        $node_id = 0;
        if (isset($data['node_id'])) {
            $node_id = (int) $data['node_id'];
        }
        if (isset($data['is_public'])) {
            $is_public = trim($data['is_public']);
            $db_data['is_public'] = empty($is_public) ? 0 : 1;
        }
        
        if (isset($data['allow_comment'])) {
            $allow_comment = trim($data['allow_comment']);
            $db_data['allow_comment'] = empty($allow_comment) ? 0 : 1;
        }
        
        if (isset($data['user_id'])) {
            $db_data['user_id'] = (int) $data['user_id'];
        }
        
        if (isset($data['rank'])) {
            $db_data['rank'] = (int) $data['rank'];
        }
        if (isset($data['color'])) {
            $color = trim($data['color']);
            if ( ! isset($color[6]) && isset($color[5])) {
                $db_data['color'] = $color;
            } 
        }
        if (isset($data['bg_color'])) {
            $bg_color = trim($data['bg_color']);
            if ( ! isset($bg_color[6]) && isset($bg_color[5])) {
                $db_data['bg_color'] = $bg_color;
            }
        }
        if (isset($data['type'])) {
            $type = trim($data['type']);
            if ($type) {
                $db_data['type'] = $type;
            }
        }
        
        if (isset($data['tag'])) {
            $tag = trim($data['tag']);
            if ($tag) {
                $db_data['tag'] = $tag;
            }
        }
        if (isset($data['layout'])) {
            $layout = trim($data['layout']);
            if ($layout) {
                $db_data['layout'] = $layout;
            }
        }
        if (isset($data['caption'])) {
            $caption = trim($data['caption']);
            if ($caption) {
                $query = tos_query_encode($caption);//$caption;
                $db_data['caption'] = $caption;
                $db_data['query'] = $query;
            }
        }
        if (isset($data['thumbnail'])) {
            $db_data['thumbnail'] = trim($data['thumbnail']);
        }
        if (isset($data['description'])) {
            $db_data['description'] = trim($data['description']);
        }
        if ($node_id > 0) {
            $this->_update_node($node_id, $db_data);
        } else {
            $node_id = $this->_insert_node($db_data);
        }
        
    }
    /* --- End Public Functions --- */
    
    
    private function _insert_node($data) {
        if ( ! isset($data['caption']) || ! isset($data['user_id']) || ! isset($data['type'])) {
            tos_die();
        }
        $now = time();
        $data['created'] = $now;
        $data['changed'] = $now;
        return $this->db->insert(
            $this->table_node,
            $data,
            $this->table_node_format
        );
    }
    
    
    private function _update_node($node_id, $data) {
        $data['changed'] = $now;
        return $this->db->update(
            $this->table_node,
            $data, $this->table_node_format
        );
    }
}
?>